package com.erp.web.controller.system;

import com.erp.framework.annotation.Log;
import com.erp.framework.core.controller.BaseController;
import com.erp.framework.core.domain.AjaxResult;
import com.erp.framework.core.page.TableDataInfo;
import com.erp.framework.enums.BusinessType;
import com.erp.framework.util.AutoFillUtils;
import com.erp.framework.util.StringUtils;
import com.erp.system.domain.Customer;
import com.erp.system.service.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FileName: CustomerController.java
 * SpringMVC控制器的详细说明
 *
 * @version 1.00
 * @Date 创建日期
 */

@Controller
@RequestMapping("/system/customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    private final String prefix = "system/customer";

    @RequiresPermissions("system:customer:view")
    @GetMapping()
    public String customer() {
        return prefix + "/customer";
    }

    @RequiresPermissions("system:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer) {
        startPage();
        List<Customer> list = customerService.queryAll(customer);
        return getDataTable(list);
    }

    /**
     * 新增客户主数据
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增客户主数据
     */
    @RequiresPermissions("system:customer:add")
    @Log(title = "新增客户主数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer) {
        if (customerService.checkCodeUnique(customer)) {
            return error("客户编码已存在，请检查！");
        }
        AutoFillUtils.setCreateInfo(customer);
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户主数据跳转页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("customer", customerService.queryById(id));
        return prefix + "/edit";
    }

    /**
     * 更新保存客户主数据
     */
    @RequiresPermissions("system:customer:edit")
    @Log(title = "更新客户主数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer) {
        if (customerService.checkCodeUnique(customer)) {
            return error("客户编码已存在，请检查！");
        }
        AutoFillUtils.setEditInfo(customer);
        return toAjax(customerService.updateCustomer(customer));
    }

    @RequiresPermissions("system:customer:remove")
    @Log(title = "删除客户主数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids) {
        return toAjax(customerService.deleteCustomerByIds(ids));
    }

    /**
     * 校验编码
     */
    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public boolean checkCodeUnique(Customer customer) {
        return StringUtils.isNotBlank(customer.getCode()) && customerService.checkCodeUnique(customer);
    }

}
